package com.jade.editor.object.editors.parts;

import java.io.File;
import java.io.FileFilter;

import org.eclipse.core.runtime.IPath;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class SettingsAni extends Dialog {

	private File _aniDir = null;
	private File[] _aniList = null;
	private String _selectedAni = "";
	private String _lastUsed = "";
	
	public File getSelectedAni(){
		for(File f : _aniList){
			if (f.getName().equals(_selectedAni))
				return f;
		}
		return null;
	}
	
	public String getLastUsedName(){
		return _lastUsed;
	}
	
	/**
	 * Create the dialog.
	 * @param parentShell
	 */
	public SettingsAni(Shell parentShell, IPath path, String lastUsed) {
		super(parentShell);
		_lastUsed = lastUsed;
		_aniDir = new File(path.toFile().getParentFile().getParent() + "//Anisotropy");
		
		FileFilter aniFilter = new FileFilter(){

			@Override
			public boolean accept(File f) {
				if(f.getPath().endsWith("ani"))
					return true;
				return false;
			}
			
		};
		
		_aniList = _aniDir.listFiles(aniFilter);
	}

	/**
	 * Create contents of the dialog.
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		container.setLayout(null);
		
		Label lblColor = new Label(container, SWT.NONE);
		lblColor.setBounds(10, 10, 55, 15);
		lblColor.setText("Color:");
		
		Composite composite = new Composite(container, SWT.NONE);
		composite.setBounds(54, 10, 184, 15);
		
		Label lblSelectAnisotropy = new Label(container, SWT.NONE);
		lblSelectAnisotropy.setBounds(10, 43, 229, 15);
		lblSelectAnisotropy.setText("Select Anisotropy:");
		
		final Combo combo = new Combo(container, SWT.NONE);
		combo.setBounds(10, 64, 228, 23);
		combo.add("");
		for(File f : _aniList){
			combo.add(f.getName());
		}
		
		for(int i = 0; i < combo.getItemCount(); i++){
			if (combo.getItem(i).equals(_lastUsed))
				combo.select(i);
		}
		
		
		combo.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (combo.getSelectionIndex() >= 0){
					_selectedAni = combo.getItem(combo.getSelectionIndex());
					_lastUsed = _selectedAni;
				}
			}
		});

		return container;
	}

	/**
	 * Create contents of the button bar.
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
				true);
		createButton(parent, IDialogConstants.CANCEL_ID,
				IDialogConstants.CANCEL_LABEL, false);
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(259, 196);
	}
}
